package cn.zj.note.view;

import cn.zj.note.MainApp;
import cn.zj.note.todo.Todo;
import cn.zj.note.util.StrFmtUtil;
import cn.zj.note.util.StringUtil;
import javafx.application.HostServices;
import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

public class TodoListCell extends ListCell<Todo> {
    private VBox vBox = new VBox(0);
    // 初始化控件和其他UI组件，例如Label、ImageView等
    private TruncatedLabel titleLabel = new TruncatedLabel(300);
    private TruncatedLabel timeLabel = new TruncatedLabel(300);
    private TruncatedLabel resourceLabel = new TruncatedLabel(300);
    private TodoDetail todoDetail;
    public TodoListCell(TodoDetail todoDetail) {
        this.todoDetail = todoDetail;
        // 构造器中初始化UI组件
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY); // 或者CONTENT_DISPLAY_CENTER
//        setAlignment(Pos.CENTER_LEFT); // 根据需要设置对齐方式
        titleLabel.setFont(new Font("Arial", 14));
        timeLabel.setFont(new Font("Arial", 8));
        timeLabel.setAlignment(Pos.BOTTOM_RIGHT);
        resourceLabel.setFont(new Font("Arial", 10));
        resourceLabel.setAlignment(Pos.BOTTOM_LEFT);
        HBox hBox = new HBox(200);
        hBox.getChildren().addAll(resourceLabel,timeLabel);
        vBox.getChildren().addAll(titleLabel, hBox);
        setGraphic(vBox);
    }

    private void updateTaskDetails(Todo todo) {
        // 这里根据Todo对象的内容更新taskDetails的UI
        // 假设taskDetails有一个setText方法用来设置详情
        this.todoDetail.update(todo);
    }

    // 重写updateItem方法以更新ListCell的内容
    @Override
    protected void updateItem(Todo todo, boolean empty) {
        super.updateItem(todo, empty);

        if (empty || todo == null) {
            // 清理或隐藏内容
            setText(null);
            setGraphic(null);
        } else {
            // 更新内容
            titleLabel.setText(todo.getTitle());
            if(todo.isStatus()){
                // 已完成
                titleLabel.setFont(Font.font(10));
                titleLabel.setStyle("-fx-text-fill: grey;");
            } else {
                titleLabel.setFont(Font.font(15));
                titleLabel.setStyle("-fx-text-fill: red;");
            }
            timeLabel.setText(StrFmtUtil.fmtDate(todo.getCreateTime()));
            resourceLabel.setText(todo.getResource());
            setGraphic(vBox);
            // 如果需要，还可以添加更多自定义逻辑，如设置背景色、字体样式等
            setOnMouseClicked(event -> {
                MainApp.setCurTodoDetail(this.todoDetail);
                if (!isEmpty()) {
                    Todo selectedTodo = getItem();
                    // 更新taskDetails的内容
                    updateTaskDetails(selectedTodo);
                }
                if (StringUtil.isNotBlank(todo.getUrl()) && event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
                    HostServices hostServices = MainApp.hostServices;
                    if (hostServices != null) {
                        hostServices.showDocument(todo.getUrl());
                    }
                }
            });
        }
    }
}